package com.xiajiucai.baiping;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import static com.xiajiucai.baiping.util.WeChatShareUtils.weChatShareUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import java.io.IOException;

public class ShareActivity extends AppCompatActivity {

  private Button shareFriendBut;
  private Button shareCircleBut;

  private String[] permissions = new String[]{
          Manifest.permission.INTERNET,
          Manifest.permission.READ_EXTERNAL_STORAGE,
          Manifest.permission.WRITE_EXTERNAL_STORAGE,
          Manifest.permission.ACCESS_NETWORK_STATE,
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_share);

    initPermission();
    weChatShareUtils.getInstance(ShareActivity.this);

    shareFriendBut = findViewById(R.id.share_friend);
    shareCircleBut = findViewById(R.id.share_circle);

    shareFriendBut.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Toast.makeText(ShareActivity.this,"分享微信好友",Toast.LENGTH_LONG).show();
      }
    });

    shareCircleBut.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        // 分享文字
        // weChatShareUtils.shareText("分享文字到朋友圈test...", SendMessageToWX.Req.WXSceneTimeline);

        // 分享图片，index == 2
        /*String imageUrl = "https://images.billionbottle.com/billionbottle/mall/8c5feed3e19144a8c3725e2d6a55bf2c_800x800.jpg?x-oss-process=image/resize,w_300/quality,q_60";
        String desc = "test context";
        String title = "test title";
        String url = "http://www.baidu.com";
        share(imageUrl, title,url , desc, 2);*/

        weChatShareUtils.share111(ShareActivity.this);
      }
    });
  }

  public void share(String imgUrl, String title, String url, String description, int index) {
    Glide.with(this).asBitmap().load(imgUrl).into(new CustomTarget<Bitmap>() {
      @SuppressLint("ClickableViewAccessibility")
      @Override
      public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
        if (index == 0) {
          weChatShareUtils.shareMiniProgram(title, resource, "百瓶", "gh_373a814ae2b7", url);
        } else if (index == 1) {
          weChatShareUtils.shareUrl(url, title, resource, description, SendMessageToWX.Req.WXSceneTimeline);
        } else {
          weChatShareUtils.sharePic(resource, SendMessageToWX.Req.WXSceneTimeline);
        }
      }
      @Override
      public void onLoadCleared(@Nullable Drawable placeholder) {
      }
    });
  }

  private void initPermission(){
    PackageManager packageManager = this.getPackageManager();
    PermissionInfo permissionInfo = null;
    for (int i = 0; i < permissions.length; i++) {
      try {
        permissionInfo = packageManager.getPermissionInfo(permissions[i], 0);
      } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
      }
      CharSequence permissionName = permissionInfo.loadLabel(packageManager);
      if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
        // 未获取权限
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
          // 这是一个坑，某些手机弹出提示时没有永不询问的复选框，点击拒绝就默认勾上了这个复选框，而某些手机上即使勾选上了永不询问的复选框也不起作用
        } else {
          ActivityCompat.requestPermissions(this, permissions, 1000);
        }
      } else {
      }
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PackageManager packageManager = this.getPackageManager();
    PermissionInfo permissionInfo = null;
    for (int i = 0; i < permissions.length; i++) {
      try {
        permissionInfo = packageManager.getPermissionInfo(permissions[i], 0);
      } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
      }
      CharSequence permissionName = permissionInfo.loadLabel(packageManager);
      if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
        Log.v("print", "您同意了【" + permissionName + "】权限");
      } else {
        Log.i("print", "您拒绝了【" + permissionName + "】权限");
      }
    }
  }

}